package com.ea.report.config;

import com.ea.common.oauth2Exception.CustomAuthExceptionEntryPoint;
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

@Configuration
@EnableResourceServer
@Profile("pro")
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
                .antMatchers(
                        "/report/result/**",
                        "/report/actuator/refresh",
                        "/report/getDashboardVo/**",
                        "/report/uploadVideo/**",
                        "/report/getReportInfo/**",
                        "/file/**",
                        "/report/uploadPhotoTemp"
                )
                .permitAll()
                // 这里配置的路径是允许这些路径是不需要身份验证就可以访问的（仅对于当前的微服务下的路径生效，对其他微服务下的路径不生效）
                .anyRequest().authenticated()
                .and().exceptionHandling();
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.authenticationEntryPoint(new CustomAuthExceptionEntryPoint());//自定义受令牌保护资源服务异常处理
    }
}
